<?php


namespace app\api\vaildate;


use app\lib\exception\ParameterException;
use think\Request;
use think\Validate;

class BaseVaildate extends Validate
{
//    公共验证函数
    public function goCheck()
    {
        $request = Request::instance();
        $parms = $request->param();
        $result = $this->batch()->check($parms);
        if (!$result) {
            $e = new ParameterException([
                'code' => 400,
                'msg' => $this->getError(),
            ]);
            throw $e;
        } else {
            return true;
        }
    }

    protected function isPostiveInteger($value, $rule = '', $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }

    protected function isNotEmpty($value, $rule = '', $data = '', $field = '')
    {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }
    }

    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function getDataByRule($array)
    {
        if (array_key_exists('uid', $array) || array_key_exists('user_id', $array)) {
            throw new ParameterException([
                'msg' => '参数包含非法参数uid或user_id'
            ]);
        }
        $newArray = array();
        foreach ($this->rule as $key => $value) {
            $newArray[$key] = $array[$key];
        }
        return $newArray;
    }
}